package com.zhehekeji.opsys.api.service.validate;

import org.apache.cxf.common.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.zhehekeji.opsys.api.code.LuckySignupResultCode;
import com.zhehekeji.opsys.api.common.util.SuperResp;
import com.zhehekeji.opsys.api.enm.ThemeMovieType;
import com.zhehekeji.opsys.api.entity.TThemeMovie;
import com.zhehekeji.opsys.api.service.validate.args.ThemeMovieValidateArgs;

/**
 * @ClassName ThemeMovieValidate
 * @Description TODO
 * @author chenweiliu chenwei.liu@163.com
 * @date 2016-3-16   下午3:41:10
 */
@Service
public class ThemeMovieValidate extends ValidateService<ValidateRets<TThemeMovie>, ThemeMovieValidateArgs>{

	private final static Logger logger = LoggerFactory.getLogger(ThemeMovieValidate.class);
	
	@Override
	public ValidateRets<TThemeMovie> validate(ThemeMovieValidateArgs validateArgs)
			throws Exception {
		ValidateRets<TThemeMovie> validateRets = new ValidateRets<TThemeMovie>();
		SuperResp resp = validateArgs.getResp();
		String movieCode = validateArgs.getMovieCode();
		if (StringUtils.isEmpty(movieCode)){
			logger.error("专题影片编码非法！");
			resp.setResultCode(LuckySignupResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc("专题影片编码非法！");
			validateRets.setSuccess(false);
			return validateRets;
		}
		TThemeMovie tmpTThemeMovie = entityDao.get(TThemeMovie.class, "o.code=?1", new Object[]{movieCode});
		if (tmpTThemeMovie == null){
			logger.error("专题影片编码非法！");
			resp.setResultCode(LuckySignupResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc("专题影片编码非法！");
			validateRets.setSuccess(false);
			return validateRets;
		}
		Integer tmpStatus = tmpTThemeMovie.getStatus();
		if (tmpStatus == 0){
			logger.error("专题影片已经停用！");
			resp.setResultCode(LuckySignupResultCode.MOVIE_HAS_BEEN_STOPPED);
			resp.setResultDesc("专题影片已经停用！");
			validateRets.setSuccess(false);
			return validateRets;
		}
		int tmpType = tmpTThemeMovie.getType() == null ? 0 : tmpTThemeMovie.getType();
		if (tmpType != ThemeMovieType.VIEW.getType()){
			logger.error("专题影片类型是观影回顾，不能报名参与观影！");
			resp.setResultCode(LuckySignupResultCode.MOVIE_IS_NOT_VIEW);
			resp.setResultDesc("专题影片类型是观影回顾，不能报名参与观影！");
			validateRets.setSuccess(false);
			return validateRets;
		}
		validateRets.setEntity(tmpTThemeMovie);
		return validateRets;
		
	}

}

